package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.NoticeTopic;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.biz.AppJumpProtocol;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.notice.AppNoticeUtil;
import com.naiterui.ehp.bp.utils.notice.AppNoticeUtil.NoticeType;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.dao.IDrNoticeInfoDao;
import com.naiterui.ehp.bs.doctor.dao.INoticeTopicDao;
import com.naiterui.ehp.bs.doctor.domain.DrNoticeInfo;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDrNoticeInfoService;
import com.naiterui.ehp.bs.doctor.utils.IMnoticeUtil;
import com.naiterui.ehp.bs.doctor.utils.ImageUrlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class AdDrNoticeInfoServiceImpl implements IDrNoticeInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdDrNoticeInfoServiceImpl.class);
    @Autowired
    private IDrNoticeInfoDao drNoticeInfoDao;
    @Autowired
    private IDoctorDao doctorDao;
    @Autowired
    private INoticeTopicDao noticTopicDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveNewDoctorMsg(Long doctorId) throws BusinessException {
        Doctor doctor = doctorDao.get(doctorId);
        if (doctor == null) {
            LOGGER.error("推送医生学院新手教程时,医生不存在doctorId:{}", doctorId);
            throw new BusinessException(ExceptionCodes.DOCTOR_NOT_EXIST);
        }
        // 如果有新手教程记录则直接返回
        DrNoticeInfo testInfo = drNoticeInfoDao.getNewDoctorNotice(doctorId, DrNoticeInfo.MSG_SCENE_NEW_DOCTOR);
        if (testInfo != null) {
            return;
        }
        DrNoticeInfo info = new DrNoticeInfo();
        info.setTitle(ConfigUtil.getString("new_user_course_message"));// 消息标题(外链的话就是内容)
        info.setContent(ConfigUtil.getString("new_user_course_url"));// 外链地址
        info.setNoticeType(DrNoticeInfo.NOTICE_TYPE_LINK_URL);// 外链
        info.setTopicId(DrNoticeInfo.TOPIC_DOCTOR_COLLEGE);// 医生学院
        info.setMsgScene(DrNoticeInfo.MSG_SCENE_NEW_DOCTOR);// 消息场景
        info.setCoverImg(ConfigUtil.getString("new_user_course_cover_img"));// 新手教程图片
        info.setDoctorId(doctorId);
        info.setCreatedAt(new Date());
        info.setChangedAt(new Date());
        // 医生学院主题对象
        NoticeTopic topic = noticTopicDao.get(DrNoticeInfo.TOPIC_DOCTOR_COLLEGE);
        // 推送消息
        String noticePath = ImageUrlUtil.genNoticePath(topic.getLogoImg());
        String targetUrl = IMnoticeUtil.getTargetUrl(topic);
        // 构建notice
        try {
            String noticeUrl = AppJumpProtocol.DR_WEB.parse(targetUrl);
            AppNoticeUtil.NoticeInfo noticeInfo = new AppNoticeUtil.NoticeInfo(info.getTitle(), System.currentTimeMillis(), noticeUrl, noticePath,
                    topic.getId(), topic.getName());
            AppNoticeUtil.sendSingleNotice(ConfigUtil.getString("amp.host.url"), doctorId, NoticeType.DR_NOTICE_INFO, noticeInfo);
            // 保存自定义公告
            info.setStartTime(new Date());// 推送日期
            info.setStatus(DrNoticeInfo.STATUS_YES);// 已推送
            drNoticeInfoDao.save(info);
        } catch (Exception e) {
            LOGGER.error("消息推送异常或更新库表状态异常");
            throw new BusinessException(ExceptionCodes.FAILED);
        }
    }
}
